#ifndef TwoStarWizard_H
#define TwoStarWizard_H
#include <QWidget>
#include <QWizard>
#include <QLabel>
#include <QCheckBox>

#include <QTableView>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QPushButton>
#include <QMetaType>
#include <QStateMachine>
#include <QState>
#include <QFinalState>

#include <inditelescope.h>
#include <indicom.h>
#include <baseclient.h>

#include <HCINDI.h>
#include "StarSelect.h"

Q_DECLARE_METATYPE(QCheckBox *);
Q_DECLARE_METATYPE(QItemSelectionModel *);

class ChooseStarsPage;
class StarsAlignPage;

class TwoStarWizard: public QWizard
{
Q_OBJECT

public slots:
 void setTelescope(INDI::BaseDevice *);


public:
  TwoStarWizard(QWidget *parent=0);
  void accept();
  void setParent(QWidget *);
  void setParent(QWidget *, Qt::WindowFlags);
  void setStarPairs(StarSelect *sp);
  void setClient(INDI::BaseClient *);
  INDI::BaseClient *getClient();
  
private:
  ChooseStarsPage *choosestarspage;
  StarsAlignPage *starsalignpage;
  INDI::Telescope *telescope;
  StarSelect *starpairs;
  INDI::BaseClient *client;
  
};

  
class IntroPage : public QWizardPage
{
Q_OBJECT

 public:
     IntroPage(QWidget *parent = 0);

 private:
     QLabel *label;
     QCheckBox *mountConnectedCheckBox;
     QCheckBox *scopeCanSync;

};

class ChooseStarsPage : public QWizardPage
{
Q_OBJECT

Q_PROPERTY(QItemSelectionModel * selectionModel READ selectionModel)

signals:
  void rowchanged(QModelIndex);
  
//public slots:
  //void changeStars(const QItemSelection &selected, const QItemSelection &deselected);

 public:
    ChooseStarsPage(QWidget* parent = 0);
    void setStarPairs(StarSelect *sp);
    void initializePage();
    QItemSelectionModel * selectionModel(void)
   {
      return starpairstableView->selectionModel();
   };   
 private:
    QLabel *choosestarslabel;
    StarSelect *starpairs;	
    QTableView *starpairstableView;
//    QModelIndex rowselected;
};

class StarsAlignPage: public QWizardPage
{
  Q_OBJECT
signals:
  void mountsynced();
  void mountslewed();
  
public slots:
  void sethasFinished();
  void syncstar1();
  void syncstar2();  
  void syncpad();
  void keepsyncstar(double ra, double dec);
  void waitgoto(double ra, double dec);
  void waitslewstar1();
  void waitslewstar2();
  void starslewed();
  void restart();
  
public:
  StarsAlignPage(QWidget *parent=0);
  void setTelescope(INDI::Telescope *t); 
  void setClient(INDI::BaseClient *c);
  void setStarPairs(StarSelect *sp);
  void initializePage();
  bool isComplete() const;
  void cleanupPage();

protected:
  void initializeStateMachine(QPushButton *);
  void slewtostar(StarSelect::star *st);
  void syncstar(StarSelect::star *st);
private:
  QVBoxLayout *layout;
  QHBoxLayout *starslayout;
  QGroupBox *star1gb, *star2gb;
  QHBoxLayout *machinelayout;
  QGroupBox *machinegb;
  
  QLabel *starsalignlabel;
  QLabel *starsalignhelp;
  QLabel *star1name, *star1RA, *star1DEC, *star1dRA, *star1dDEC;
  QLabel *star2name, *star2RA, *star2DEC, *star2dRA, *star2dDEC;
  INDI::Telescope *telescope;
  INDI::BaseClient *client;
  StarSelect *starpairs;
  std::pair<StarSelect::star *, StarSelect::star *> spair;
  struct StarSelect::star *star1, *star2;
  double star1syncra, star1syncde;
  double star2syncra, star2syncde;
  double initialsyncra, initialsyncde;
  double syncedra, syncedde;
  QStateMachine starsalignstatemachine;
  QState init, slewStar1, waitforSlewStar1, syncStar1, waitsyncStar1, slewStar2, waitforSlewStar2, waitsyncStar2;
  QFinalState syncStar2;
  QPushButton *statebutton;
  bool hasfinished;
};

#endif